package com.shanghaiyimu.yimiyangche.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.adapter.TabFragmentAdapter;
import com.shanghaiyimu.yimiyangche.fragment.MyFragment1;

import java.util.ArrayList;
import java.util.List;

public class GaiZhuangActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private String[]  titles = new String[6];
    private FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gai_zhuang);
        initView();

    }
    private void initView() {


        titles[0]  = "轮毂改装";
        titles[1]  = "外饰改装";
        titles[2]  = "悬挂改装";
        titles[3]  = "音响隔音";
        titles[4]  = "电动系列";
        titles[5]  = "底盘改装";
        fragmentManager = getSupportFragmentManager();
        viewPager = (ViewPager) findViewById(R.id.viewPager_gaizhuang);
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            Fragment fragment = new MyFragment1();
            Bundle bundle = new Bundle();
            bundle.putString("text",titles[i]);
            fragment.setArguments(bundle);
//            fragments.add( MyFragment1.newInstance(titles[i]));
            fragments.add( fragment);
        }
        TabFragmentAdapter tabFragmentAdapter = new TabFragmentAdapter(fragments, titles, fragmentManager, this);
        viewPager.setAdapter(tabFragmentAdapter);
        // 初始化
        tabLayout = (TabLayout) findViewById(R.id.tablayout_gaizhuang);

        // 设置tab文本的没有选中（第一个参数）和选中（第二个参数）的颜色
        tabLayout.setTabTextColors(Color.BLACK, Color.RED);
        //设置tab的模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);

//        //添加tab选项卡
//        for (int i = 0; i < titles.length; i++) {
//            tabLayout.addTab(tabLayout.newTab().setText("Tab"+i));
//        }
        // 将ViewPager和TabLayout绑定
        tabLayout.setupWithViewPager(viewPager);
    }
}
